package ru.vsu.cs.pm.pages;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation;
import org.apache.wicket.extensions.markup.html.tree.table.IColumn;
import org.apache.wicket.extensions.markup.html.tree.table.PropertyRenderableColumn;
import org.apache.wicket.extensions.markup.html.tree.table.PropertyTreeColumn;
import org.apache.wicket.feedback.ComponentFeedbackMessageFilter;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import ru.vsu.cs.pm.BasePage;
import ru.vsu.cs.pm.PMSession;
import ru.vsu.cs.pm.beans.ItemManager;
import ru.vsu.cs.pm.components.tree.MutableTreeNode;
import ru.vsu.cs.pm.components.tree.MutableTreeTable;
import ru.vsu.cs.pm.models.Item;

import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import java.util.List;

/**
 * User: ohodyakov
 * Date: 27.12.2010
 * Time: 14:04:27
 */
@MountPath(path = "items")
public class ItemListPage extends BasePage {
    @SpringBean
    private ItemManager itemManager;

    private MutableTreeTable tree;

    public ItemListPage() {
        IColumn[] columns = new IColumn[] {
                new PropertyTreeColumn(new ColumnLocation(ColumnLocation.Alignment.MIDDLE, 8, ColumnLocation.Unit.PROPORTIONAL),
                        "Title", "userObject.title"),
                new PropertyRenderableColumn(new ColumnLocation(ColumnLocation.Alignment.MIDDLE, 8, ColumnLocation.Unit.PROPORTIONAL), "Description",
                        "userObject.description"),
        };
        MutableTreeNode rootNode = new MutableTreeNode();
        List<Item> rootItems = itemManager.getRootItemsForUser(PMSession.get().getAuthenticatedUser());
        MutableTreeNode child = null;
        for (Item item : rootItems) {
            child = new MutableTreeNode(item);
            child.setAllowsChildren(item.getChildrenCount() > 0);
            rootNode.add(child);
        }
        DefaultTreeModel model = new DefaultTreeModel(rootNode);
        model.setAsksAllowsChildren(true);
        tree = new MutableTreeTable("treeTable", new CompoundPropertyModel(model), columns) {

        };
        tree.setRootLess(true);
        add(tree);

        final ItemForm itemForm = new ItemForm("itemForm");
        itemForm.setOutputMarkupId(true);
        itemForm.setOutputMarkupPlaceholderTag(true);
        add(itemForm);
        itemForm.setVisible(false);

        add(new AjaxLink("addLink") {
            @Override
            public void onClick(AjaxRequestTarget ajaxRequestTarget) {
                TreeNode treeNode = (TreeNode) tree.getTreeState().getSelectedNodes().iterator().next();
                if (treeNode != null) {
                    itemForm.setParentNode((MutableTreeNode) treeNode);
                    Item newItem = new Item();
                    itemForm.setItem(newItem);
                    itemForm.setVisible(true);

                    ajaxRequestTarget.addComponent(itemForm);
                }
            }
        });

    }

    private class ItemForm extends Form<Item> {

        private MutableTreeNode parentNode;

        public ItemForm(String id) {
            super(id);

            TextField<String> titleField = new TextField<String>("title");
            titleField.setRequired(true);
            add(titleField);

            TextField<String> descriptionField = new TextField<String>("description");
            descriptionField.setRequired(true);
            add(descriptionField);

            add(new FeedbackPanel("itemFeedback", new ComponentFeedbackMessageFilter(this)));

            AjaxSubmitLink cancelBtn = new AjaxSubmitLink("cancelButton", this){
                @Override
                protected void onSubmit(AjaxRequestTarget ajaxRequestTarget, Form<?> form) {
                    ItemForm.this.setVisible(false);
                    ajaxRequestTarget.addComponent(ItemForm.this);
                }
            };

            AjaxSubmitLink saveLink = new AjaxSubmitLink("saveButton", this) {
                @Override
                protected void onSubmit(AjaxRequestTarget ajaxRequestTarget, Form<?> form) {
                    Item item = getModelObject();
                    item.setUser(PMSession.get().getAuthenticatedUser());
                    Item parentItem = (Item) parentNode.getUserObject();
                    item.setType(parentItem.getType());
                    item.setParent(parentItem);
                    item.setChildrenCount(0);
                    item.setRootItem(false);

                    itemManager.saveOrUpdateItem(item);

                    ItemForm.this.setVisible(false);
                    parentNode.setAllowsChildren(true);
                    ajaxRequestTarget.addComponent(ItemForm.this);
                    tree.expandParent(parentNode);
                    tree.updateTree(ajaxRequestTarget);
//                    ajaxRequestTarget.addComponent(tree);
                }
            };

            cancelBtn.setDefaultFormProcessing(false);
            add(cancelBtn);

            add(saveLink);
        }

        public MutableTreeNode getParentNode() {
            return parentNode;
        }

        public void setParentNode(MutableTreeNode parentNode) {
            this.parentNode = parentNode;
        }

        public void setItem(Item item) {
            setModel(new CompoundPropertyModel<Item>(item));
        }
    }
}
